package com.dy.seleniumlearn;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

public class WebElementOp {
    WebDriver driver;
    WebDriver.Navigation navigation;

    @BeforeMethod
    public void SetUp() {
        System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
        driver = new FirefoxDriver();
        navigation = driver.navigate();
    }

    @Test
    public void test() throws InterruptedException, IOException {
        driver.get("http://39.101.198.110/testhtml/exam/");
        WebElement link = driver.findElement(By.tagName("a"));
        link = driver.findElement(By.linkText("继续提供房源信息"));
        link = driver.findElement(By.partialLinkText("房源信息"));
        link.click();
        Thread.sleep(3000);
        WebElement username = driver.findElement(By.className("uname"));
        username.clear();
        username.sendKeys("交租");
        link = driver.findElement(By.cssSelector("#email"));
        //输入框清空
        link.clear();
        link.sendKeys("123@qq.com");
        link = driver.findElement(By.xpath("//*[@id=\"email\"]"));
        link.clear();
        link.sendKeys("123@qq.com");
        link = driver.findElement(By.cssSelector("[name='c1']"));
        link.click();
        Thread.sleep(3000);
        //双击输入框使其变红
        Actions action = new Actions(driver);
        action.doubleClick(username).build().perform();
        WebElement list1 = driver.findElement(By.cssSelector("[name='list1']"));
        //选择框设置
        Select select = new Select(list1);
        //通过索引设置目前选项
        select.selectByIndex(0);
        //通过value属性设置目前选项
        select.selectByValue("b");
        WebElement list2 = driver.findElement(By.cssSelector("[name='list2']"));
        Select select1 = new Select(list2);
        //通过text设置目前选项
        select1.selectByVisibleText("4000");
        for (WebElement option : select.getOptions()) {
            if (option.getText().equals("公寓")) {
                option.click();
            }
        }
        System.out.println("select是否支持多选;" + select.isMultiple());
        System.out.println("select1是否支持多选;" + select1.isMultiple());
        //文件上传
        link = driver.findElement(By.cssSelector("[type='file']"));
        link.sendKeys("D:\\jdk\\jdk-15.0.1_linux-x64_bin.rpm");
        //截屏功能
        File FileiImage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(FileiImage, new File("D:\\image.png"));

    }

    @AfterMethod
    public void afterMethod() {
        //driver.close();
    }
}
